Thread: Graphics.h [ Canvas class ]

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    netherlands
    Posts
    2

    Graphics.h [ Canvas class ]

    this is a try at a win32 graphics lib

    i dont got it all

    improvements , sugesetions and help are welkome

    Code:
    //bluatigro 29 dec 2016
    //win32 graphics class
    #include <windows.h> 
    class Canvas
    {
    public :
      HDC hdc ;
      Canvas(){}
      ~Canvas(){}
      void ellipse( 
        int mx , int my , int dx , int dy ,
        int clrin , int clrout , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
     
        Ellipse( hdc , mx - dx / 2 , my - dy / 2
          , mx + dx / 2 , my + dy / 2 ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
      void line( 
        int x1 , int y1 , int x2 , int y2 ,
        int clr , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clr ) ;
        HPEN oldpen = (HPEN)SelectObject( hdc , newpen ) ;
        MoveTo( hdc , x1 , y1 , NULL ) ;
        LineTo( hdc , x2 , y2 ) ;
        SelectObject( hdc , oldpen ) ;
        DeleteObject( newpen ) ;
      }
      void triangle( 
        int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ,
        int clrin , int clrout , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
        POINT p[] = { { x1 , y1 } , 
                    { x2 , y2 } , 
                    { x3 , y3 } } ;
      
        Polygon( hdc , p , 3 ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
      void rectangle( HDC hdc ,
        int left , int top , int right , int bottom ,
        int clrin , int clrout , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
        Rectangle( hdc , left , top , right , bottom ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
    } ;
    Canvas canvas ;
    // primary colors
    const int BLACK   = 0x000000 ;
    const int RED     = 0x0000ff ;
    const int GREEN   = 0x00ff00 ;
    const int YELLOW  = 0x00ffff ;
    const int BLUE    = 0xff0000 ;
    const int MAGENTA = 0xff00ff ;
    const int CYAN    = 0xffff00 ;
    const int WHITE   = 0xffffff ;
    //  mixed colors
    const int PINK    = 0x7f7fff ;
    const int ORANGE  = 0x007fff ;
    const int PURPLE  = 0x7f007f ;
    const int GRAY    = 0x7f7f7f ;
      
    int colorMix( int clr1 , float f , int clr2 )
    {
      int r1 , g1 , b1 , r2 , g2 , b2 ;
      r1 = clr1 & 0xff ;
      g1 = ( clr1 >> 8 ) & 0xff ;
      b1 = ( clr1 >> 16 ) & 0xff ;
      r2 = clr2 & 0xff ;
      g2 = ( clr2 >> 8 ) & 0xff ;
      b2 = ( clr2 >> 16 ) & 0xff ;
      r1 = r1 + ( r2 - r1 ) * f ;
      g1 = g1 + ( g2 - g1 ) * f ;
      b1 = b1 + ( b2 - b1 ) * f ;
      return r1 + g1 * 0x100 + b1 * 0x10000 ; 
    }

  2. #2
    Registered User
    Join Date
    Apr 2015
    Location
    netherlands
    Posts
    2

    Canvas class

    update :
    renaming to Canvas.h
    splitting into .h and .cpp

    Canvas.h
    Code:
    //bluatigro 29 dec 2016
    //Canvas.h
    //win32 graphics class
    #include <windows.h> #ifndef CANVAS_H
    #define CANVAS_H
    class Canvas
    {
    public :
      HDC hdc ;
      Canvas(){}
      ~Canvas(){}
      void ellipse( 
        int mx , int my , int dx , int dy ,
        int clrin , int clrout , int thick , bool center = false ) ;
      void line( 
        int x1 , int y1 , int x2 , int y2 ,
        int clr , int thick ) ;
      void triangle( 
        int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ,
        int clrin , int clrout , int thick ) ;
      void rectangle( 
        int left , int top , int right , int bottom ,
        int clrin , int clrout , int thick , bool center = false ) ;
    } ;
    Canvas canvas ;
    // primary colors
    const int BLACK   = 0x000000 ;
    const int RED     = 0x0000ff ;
    const int GREEN   = 0x00ff00 ;
    const int YELLOW  = 0x00ffff ;
    const int BLUE    = 0xff0000 ;
    const int MAGENTA = 0xff00ff ;
    const int CYAN    = 0xffff00 ;
    const int WHITE   = 0xffffff ;
    //  mixed colors
    const int PINK    = 0x7f7fff ;
    const int ORANGE  = 0x007fff ;
    const int PURPLE  = 0x7f007f ;
    const int GRAY    = 0x7f7f7f ;
      
    int colorMix( int clr1 , float f , int clr2 ) ;
    #endif
    Canvas.cpp
    Code:
    /
    //bluatigro 29 dec 2016
    //Canvas.cpp
    //win32 graphics class#include "Canvas.h"
      void Canvas::ellipse( 
        int mx , int my , int dx , int dy ,
        int clrin , int clrout , int thick , bool center )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
        if ( center ) 
        {
          Ellipse( hdc , mx - dx / 2 , my - dy / 2
            , mx + dx / 2 , my + dy / 2 ) ;
        }
        else
          Ellipse( hdc , mx , my , dx , dy ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
      void Canvas::line( 
        int x1 , int y1 , int x2 , int y2 ,
        int clr , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clr ) ;
        HPEN oldpen = (HPEN)SelectObject( hdc , newpen ) ;
        MoveTo( hdc , x1 , y1 , NULL ) ;
        LineTo( hdc , x2 , y2 ) ;
        SelectObject( hdc , oldpen ) ;
        DeleteObject( newpen ) ;
      }
      void Canvas::triangle( 
        int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ,
        int clrin , int clrout , int thick )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
        POINT p[] = { { x1 , y1 } , 
                      { x2 , y2 } , 
                      { x3 , y3 } } ;
      
        Polygon( hdc , p , 3 ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
      void rectangle(
        int x1 , int y1 , int x2 , int y2 ,
        int clrin , int clrout , int thick , bool center )
      {
        HPEN newpen = CreatePen( PS_SOLID , thick , clrout ) ;
        HBRUSH newbrush = CreateSolidBrush( clrin ) ;
        HPEN oldpen = (HPEN) SelectObject( hdc , newpen ) ;
        HBRUSH oldbrush = (HBRUSH) SelectObject( hdc , newbrush ) ;
        if ( center )
          Rectangle( hdc , x1 - x2 / 2 , y1 - y2 / 2 , x1 + x2 / 2 , y1 + y2 / 2 ) ;
        else
          Rectangle( hdc , x1 , y1 , x2 , y2 ) ;
        SelectObject( hdc, oldpen ) ;
        SelectObject( hdc, oldbrush ) ;
        DeleteObject( newpen ) ;
        DeleteObject( newbrush ) ;
      }
    int colorMix( int clr1 , float f , int clr2 )
    {
      int r1 , g1 , b1 , r2 , g2 , b2 ;
      r1 = clr1 & 0xff ;
      g1 = ( clr1 >> 8 ) & 0xff ;
      b1 = ( clr1 >> 16 ) & 0xff ;
      r2 = clr2 & 0xff ;
      g2 = ( clr2 >> 8 ) & 0xff ;
      b2 = ( clr2 >> 16 ) & 0xff ;
      r1 = r1 + ( r2 - r1 ) * f ;
      g1 = g1 + ( g2 - g1 ) * f ;
      b1 = b1 + ( b2 - b1 ) * f ;
      return r1 + g1 * 0x100 + b1 * 0x10000 ; 
    }
    Last edited by bluatigro; 12-30-2016 at 05:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. repainting a drawing canvas
    By neandrake in forum Windows Programming
    Replies: 2
    Last Post: 09-09-2006, 02:51 PM
  2. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  3. Builder Canvas & TListBox question
    By Joan Lesas in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2003, 01:13 AM
  4. Selecting things on the canvas
    By Barjor in forum Windows Programming
    Replies: 0
    Last Post: 08-30-2001, 02:10 PM

Tags for this Thread